package com.dq.controller.api.merchant.workbench.customer;

import com.dq.exception.customException.AdminException;
import com.dq.controller.in.orders.CusSaleQuery;
import com.dq.vo.ListVo;
import com.dq.vo.Vo;
import com.dq.domain.orders.UserOrder;
import com.dq.domain.orders.info.OrderInfo;
import com.dq.service.AllService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liuhui
 * @date 2019/11/25 0025 11:28:57
 */
@RestController
@RequestMapping("/api/merchant/workbench/customer/sale")
@Api(description = "顾客销售单")
public class CusSaleController extends AllService {

    @PostMapping("/list")
    @ApiOperation("多条件检索顾客销售单")
    public Vo list(@RequestBody CusSaleQuery cusSaleQuery)throws AdminException{
        List<UserOrder> userOrderList = cusSaleService.findByWhere(cusSaleQuery);
        Long totalCount = cusSaleService.countByWhere(cusSaleQuery);
        List<UserOrder.OrgSaleListVo> orgSaleListVos = new ArrayList<>();
        userOrderList.forEach(userOrder -> {orgSaleListVos.add(userOrder.toOrgSaleListVo());});
        PageInfo pageInfo = new PageInfo(orgSaleListVos);
        pageInfo.setTotal(totalCount);
        return new Vo(pageInfo);
    }

    @GetMapping("/detail/{id}")
    @ApiOperation("订单详情商品列表")
    public Vo detailGoodsList(@PathVariable Long id)throws AdminException{
        UserOrder userOrder = dao.get(UserOrder.class,id);
        if(null!=userOrder){throw new AdminException("该订单不存在");}
        List<OrderInfo> orderInfoList = orderInfoRepository.findByOrders(userOrder);
        List<OrderInfo.OrgOrderInfoVo> orgOrderInfoVos = new ArrayList<>();
        orderInfoList.forEach(orderInfo -> {orgOrderInfoVos.add(orderInfo.toOrgOrderInfoVo());});
        PageInfo pageInfo = new PageInfo(orgOrderInfoVos);
        return new Vo(pageInfo);
    }
}
